<?php

class User_StoreController extends Model3_Controller
{

    public function indexAction()
    {
        if (!Model3_Auth::isAuth())
        {
            $this->redirect('Index/index');
        }
        
        $em = $this->getEntityManager('DefaultDb');
        $this->view->categories = $em->getRepository('DefaultDb_Entities_Category')->findAll();
    }

    public function viewCatalogAction()
    {
        $em = $this->getEntityManager('DefaultDb');

        $catalogId = $this->getRequest()->getParam('id');
        $catalog = $em->find('DefaultDb_Entities_Catalog', $catalogId);
        $this->view->catalog = $catalog;
        $this->view->user = $catalog->getClient();
        $this->view->category = $this->view->user->getCategory();
    }

    public function viewCartAction()
    {   
        $commerce = new Model3_Session_Namespace('commerce');
        $cart = $commerce->cart;
        $idTienda = $this->getRequest()->getParam('id');
        $action = $this->getRequest()->getAction();
        
        $em = $this->getEntityManager('DefaultDb');
        
        $branchesRepos = $em->getRepository('DefaultDb_Entities_BranchesUser');
        $userRepos = $em->getRepository('DefaultDb_Entities_User');

        if($cart)
        {
            $credentials = Model3_Auth::getCredentials();
            if($credentials['type'] == DefaultDb_Entities_User::USER_DRIVER)
            {
                $viewUser = new Model3_Session_Namespace('viewUser');
                $buyer = $userRepos->find($viewUser->id);
            }
            else
            {
                $buyer = $userRepos->find($credentials['id']);
            }
            $tienda =$em->find('DefaultDb_Entities_User', $cart->getIdSeller());
            $this->view->tiendaSelected = $tienda;
            $branchesBuyer = $branchesRepos->findBy(array('client'=>$buyer));
            $branchesSeller = $branchesRepos->findBy(array('client' => $cart->getIdSeller()));
            $this->view->branchesSeller = $branchesSeller;
            $this->view->branchesBuyer = $branchesBuyer;
        }
        if($idTienda)
        {
            $branchesSeller = $branchesRepos->findBy(array('client'=>Model3_Auth::getCredentials('id')));
            $branchesBuyer = $branchesRepos->findBy(array('client' => $idTienda));
            $this->view->branchesSeller = $branchesSeller;
            $this->view->branchesBuyer = $branchesBuyer;
            $tienda = $em->find('DefaultDb_Entities_User', $idTienda);
            $this->view->tiendaSelected = $tienda;
        }
        $this->view->action = $action;

    }

    public function confirmOrderAction()
    {
        $commerce = new Model3_Session_Namespace('commerce');
        $paymentResult = false;
        $cart = $commerce->cart;
        $post = $this->getRequest()->getPost();
        if($cart || isset($post['store']))
        {
            $em = $this->getEntityManager('DefaultDb');
            $favoriteUserRepos = $em->getRepository('DefaultDb_Entities_FavoriteUsers');
            
            $credentials = Model3_Auth::getCredentials();
            if($credentials['type'] == DefaultDb_Entities_User::USER_DRIVER)
            {
                $em = $this->getEntityManager('DefaultDb');
                $viewUser = new Model3_Session_Namespace('viewUser');
                $buyerId = $viewUser->id;
            }
            else
            {
                $buyerId = $credentials['id'];
            }
            
            $buyer = $em->find('DefaultDb_Entities_User', $buyerId);
            $pass = true;
            if(Model3_Auth::getCredentials('type') == DefaultDb_Entities_User::USER_DRIVER && $buyer)
            {
                $pass = false;
                if($this->getRequest()->isPost())
                {
                    if(isset($post['pass']))
                    {
                        $pass = $post['pass'];
                        if($buyer->getPassword() == md5($pass))
                        {
                            $pass = true;
                        }
                    }
                }
            }
            
            if($buyer && $pass)
            {
                
                if($cart)
                    $seller = $em->find('DefaultDb_Entities_User', $cart->getIdSeller());
                else{
                    $post = $this->getRequest()->getPost();
                    if(isset($post['store'])){
                        //$usersAdapter->findOneBy(array('id'=>$post['currentUser']));
                        
                        $seller =$em->getRepository('DefaultDb_Entities_User')->findOneBy(array('id'=>$post['store']));
                        
                        }
                }
                //$seller = $em->find('DefaultDb_Entities_User', $cart->getIdSeller());
                //$post = $this->getRequest()->getPost();
                $order = new DefaultDb_Entities_M3CommerceOrder();
                $order->setBuyer($buyer);
                $order->setSeller($seller);
                $order->setCreationDate(new DateTime());
                if(isset($post['comments']))
                {
                    $order->setComments($post['comments']);
                }
                else
                    $order->setComments ('');
                if(isset($post['pointBuyer']))
                {
                    $pointRepos = $em->getRepository('DefaultDb_Entities_BranchesUser');
                    $pointBuyer = $pointRepos->find($post['pointBuyer']);
                    $order->setPointBuyer($pointBuyer->getPoint());
                }
                if($cart){
                    //Si tiene productos o carrito agregamos los productos a la orden
                    foreach($cart->getItems() as $id => $item)
                    {
                        $product = $em->find('DefaultDb_Entities_Product', $id);
                        if($product)
                        {
                            if(isset($item['variant']) && count($item['variant']) > 0)
                            {
                                foreach($item['variant'] as $idVariant => $itemVariant)
                                {
                                    $productToOrder = new DefaultDb_Entities_M3CommerceProductToOrder();
                                    $productToOrder->setProduct($product);
                                    $variant = $em->find('DefaultDb_Entities_ProductVariants',$idVariant);
                                    $productToOrder->setVariant($variant);
                                    $variant->setStock($variant->getStock()-$itemVariant['quantity']);
                                    $productToOrder->setQuantity($itemVariant['quantity']);
                                    $productToOrder->setPrice($product->getPrice() * $itemVariant['quantity']);
                                    $order->addProduct($productToOrder);
                                }
                            }
                            else
                            {
                                $productToOrder = new DefaultDb_Entities_M3CommerceProductToOrder();
                                $productToOrder->setProduct($product);
                                $product->setStock($product->getStock()-$item['quantity']);
                                $productToOrder->setQuantity($item['quantity']);
                                $productToOrder->setPrice($product->getPrice() * $item['quantity']);
                                $order->addProduct($productToOrder);
                            }

                        }
                    }
                }
                //Agregamos a favoritos al del cliente al vendedor
                $helperFavoriteUsers = new Helper_FavoriteUsers($this->getEntityManager('DefaultDb'));
                $helperFavoriteUsers->addFavorite($buyer->getId(), $seller->getId(),  DefaultDb_Entities_FavoriteUsers::FAVORITE_SELLER);
                //Agregamos como cliente favorito al provedor 
                $helperFavoriteUsers->addFavorite($seller->getId(), $buyer->getId(), DefaultDb_Entities_FavoriteUsers::FAVORITE_BUYER);
                $em->persist($order);
                $em->flush();
                $commerce->cart = false;
                
                $paymentResult = true;
            }
        }
        else{
            header ('Location:'.$this->view->url(array('controller'=>'Store','action'=>'index','module'=>'User')));
        }
        $this->view->payment_result = $paymentResult;
    }

    public function reviewAction()
    {
        $commerce = new Model3_Session_Namespace('commerce');
        $cart = $commerce->cart;
        $neg = !NULL;
        $action = $this->getRequest()->getAction();
        $em = $this->getEntityManager('DefaultDb');
        $branchesRepos = $em->getRepository('DefaultDb_Entities_BranchesUser');
        $scheduleAdapter = $em->getRepository('DefaultDb_Entities_Schedule');
        $userRepos = $em->getRepository('DefaultDb_Entities_User');
        $idSeller = "";
        $credentials = Model3_Auth::getCredentials();
        if($credentials['type'] == DefaultDb_Entities_User::USER_DRIVER)
        {
            $viewUser = new Model3_Session_Namespace('viewUser');
            $buyer = $userRepos->find($viewUser->id);
        }
        else
        {
            $buyer = $userRepos->find($credentials['id']);
        }
        if($cart)
        {         
            $idSeller = $cart->getIdSeller();
            $tienda =$em->find('DefaultDb_Entities_User', $cart->getIdSeller());
            $this->view->tiendaSelected = $tienda;
            $branchesBuyer= $branchesRepos->findBy(array('client'=>$buyer));
            $branchesSeller = $branchesRepos->findBy(array('client' => $idSeller));
            $this->view->branchesBuyer = $branchesBuyer;
            $this->view->branchesSeller = $branchesSeller;
        }
        if($this->getRequest()->isPost())
        {
            $post= $this->getRequest()->getPost();
            $this->view->comments= $post['comment'];
            $tienda =$em->find('DefaultDb_Entities_User', $post['store']);
            $this->view->tiendaSelected = $tienda;
            $idSeller = $tienda->getId();
            $branchesBuyer = $branchesRepos->findBy(array('client'=>$buyer));
            $branchesSeller = $branchesRepos->findBy(array('client' => $idSeller));
            $this->view->branchesBuyer = $branchesBuyer;
            $this->view->branchesSeller = $branchesSeller;
        }
        else{
            if(!$cart)
                header ('Location:'.$this->view->url(array('controller'=>'Store','action'=>'index','module'=>'User')));
        }
        if(Model3_Auth::getCredentials('type') == DefaultDb_Entities_User::USER_DRIVER)
        {
            $em = $this->getEntityManager('DefaultDb');
            $viewUser = new Model3_Session_Namespace('viewUser');
            $this->view->viewUser = $em->find('DefaultDb_Entities_User', $viewUser->id);
        }
        $this->view->getJsManager()->addJsVar('urlGetSchedules', '\'' . $this->view->url(array('module' => '', 'controller' => 'Ajax_UserShipping', 'action' => 'getSchedulesByOrigin')) . '\'');
        $this->view->getJsManager()->addJsVar('urlSelectSchedule','\''.$this->view->url(array('module' => '', 'controller' => 'Ajax_UserShipping', 'action' => 'calculateShippingPerRoute')).'\'');
        $this->view->getJsManager()->addJsVar('idSeller','\''.$idSeller.'\'');
        $this->view->action = $action;
    }

    public function dmReviewAction()
    {
        //$this->initialize();
        $this->view->getCssManager()->addCss('blueprint/screen.css');
        $this->view->getJsManager()->addJs('templates/dineromail' . JS_POSFIX . '.js');
        $this->view->nomFix = $this->getParam(0);
        $siteAdapter = new Model_Sitios();
        $helper = new Helper_DisplayHelper();
        $this->view->reviewCart = true;
        $this->view->method = false;

        if($this->view->nomFix == '')
        {
            $sitio = $helper->getSiteByDomain();
        }
        else
            $sitio = $siteAdapter->getSiteByNameOnly($this->view->nomFix, SELECTOR);

        $this->prepareView();
        $tiendaAdapter = new Data_Tiendas();
        $this->view->configTienda = $tiendaAdapter->findOne('id_sitio', $sitio['id']);
        $helperPaypal = new Helper_paypalResponse($sitio['id']);
        if(PRODUCCION)
            $this->view->url = $sitio['dominio'] != '' ? $sitio['dominio'] . '/confirmDmOrder' : 'http://' . $sitio['nombre_fix'] . '.storesfactory.com/confirmDmOrder/' . $sitio['nombre_fix'];
        else
            $this->view->url = 'http://localhost/storesfactory/public/Display/confirmDmOrder/' . $sitio['nombre_fix'];

        if($this->isPost())
        {
            $post = $this->getPost();
            $this->view->response = $helperPaypal->buildEmptyResponse();
            $this->view->method = $post['method'];
        }
    }

    private function prepareView()
    {
        $action = $this->view->action;
        $this->view->idUsuario = $this->view->showCart = false;

        foreach($widgets as $widget)
        {
            if($widget['tipo'] == WIDGET_CATALOGS)
            {
                $this->view->getCssManager()->addCss('tienda' . CSS_POSFIX . '.css');
            }

            if($widget['tipo'] == TRADUCTOR)
                $this->view->loadGoogleTranslateAPI = true;
        }

        $paginasAdapter = new Model_VistaPaginas();
        $page = $paginasAdapter->getPageByNameBySitio($this->_nomFixPage, $this->_sitio['id']);
        if($page && $page->categoria == PAGINA_TIENDA)
        {
            $this->view->linkNav = '/' . $page->nombre_fix;
            $this->view->showCart = true;
            $idSitio = $this->_sitio['id'];
            $this->view->idSitio = $idSitio;
            $this->view->nomFix = $this->_sitio['nombre_fix'];
            $this->view->nomPageFix = $this->_nomFixPage;
            $this->view->tipo_template = $this->_sitio['tipo_template'];
            $this->view->template = $this->_sitio['template'];

            //consultar productos para imprimirlos
            $this->getProductsInitials($idSitio);
            //$this->view->productos = $productos;
            //var_dump($productos);


            $tagsGroups = array();
            //var_dump($productos);
            $exceptTagsName = '';

            switch($tienda->fuente_productos_iniciales)
            {
                case Data_Catalogos::INICIO_CATALOGO:
                    $tagsGroups = $this->getTagGroupNames($idSitio); //Da todos los grupos
                    $tagsGroups = $this->cleanGroups($tagsGroups, Data_Catalogos::INICIO_CATALOGO, $idSitio); //limpia dependiendo del catalogo
                    break;
                case Data_Catalogos::INICIO_ETIQUETAS:
                    $exceptTagsName = $this->getInitTagsName($tienda);
                    $this->view->tagsIni = $tienda->valor_productos_iniciales;

                    $tagsGroups = $this->getTagGroupNamesSelected($idSitio, $exceptTagsName); //debe poder tomar todos execto los que ya tomo y quitando los que ya
                    $tagsGroups = $this->cleanGroups($tagsGroups, Data_Catalogos::INICIO_ETIQUETAS, $idSitio, $exceptTagsName);

                    break;
                case Data_Catalogos::INICIO_DESTACADOS:
                    $tagsGroups = $this->getTagGroupsDestacados($idSitio);
                    $tagsGroups = $this->cleanGroups($tagsGroups, Data_Catalogos::INICIO_DESTACADOS, $idSitio);
                    break;
            }
            ////obtener las etiquetas de cada grupo y almacenarlas en un arreglo

            $this->view->tagsNameSelected = $exceptTagsName;
            $this->view->tagsGroups = $tagsGroups;
            $this->view->getCssManager()->addCss('tienda' . CSS_POSFIX . '.css');
        }

        //se inicializan los valores para el template Avanzado
        if($this->_sitio['tipo_template'] == 2)
        {
            $templateHelper = new Helper_TemplateAvanzado();
            $layout = '';
            if($action != 'viewroomcart' && $action != 'viewcart' && $action != 'confirmorder' && $action != 'review' && $action != 'confirmDmOrder')
            {
                //if( $this->view->pagina['categoria'] != PAGINA_TIENDA )
                $layout = $this->view->pagina['layout'];
            }

            $toInclude = $templateHelper->initAdvancedTemplate($this->_sitio, $layout);
            $this->view->css = $toInclude['css'];
            $this->view->js = $toInclude['js'];

            //Estilos para los sitios de template con plantilla propia,
            //se agregan los estilos de contacto y de comentarios y otros...
            $this->cssTemplateAvanzado($toInclude);
        }
        else
        {
            /**
             * Esta seccion se agrega para poder agregar los archivos css y js del modo avanzado al modo personalizado
             */
            $templateHelper = new Helper_TemplateAvanzado();
            $toInclude = $templateHelper->initAdvancedTemplate($this->_sitio, '');
            $this->view->css = $toInclude['css'];
            $this->view->js = $toInclude['js'];

            $this->setLayoutToTemplate();
        }

        //Si es una accion de tienda no se llaman los elementos
        if($action != 'viewroomcart' && $action != 'viewcart' && $action != 'confirmorder' && $action != 'confirmDmOrder' /* && $action != 'billing' */ && $action != 'review')
        {
            if($this->view->pagina)
            {
                //obtenemos los elementos
                if(!$this->view->elementMode)
                    $this->getElements();
                else
                    $this->getSingleElement();
            }
        }
    }

    /**
     * Crea un elemento temporal con id de elemento = -1
     * @param int $idSitio
     * @param int $tipoElemento
     * @param string $contenido Para el elemento catalogo es el tipo de vista del catalogo(lista, o 
     * @param int $id
     * @return array Arreglo asociativo con los campos 'contenido', 'id'=>-1
     */
    protected function createTemporalElement($idSitio, $tipoElemento, $contenido, $id)
    {
        return Helper_Sites::createTemporalElement($idSitio, $tipoElemento, $contenido, $id);
    }

    /**
     * Regresa los productos que seran mostrados al inicio de la tienda
     * segun la confoguracion definida por el usuario
     * @param int $idSitio
     * @return array|false
     */
    private function getProductsInitials($idSitio)
    {
        $tiendasAdapter = new Data_Tiendas();
        $catalogosAdapter = new Data_Catalogos();
        $productosAdapter = new Data_Productos();
        $tienda = $tiendasAdapter->getStoreForASite($idSitio);
        $numCatalog = $catalogosAdapter->getNumCatalogbySitioId($idSitio);
        $numProducts = $productosAdapter->getNumProductsBySitioId($idSitio, 1);

        if(is_array($tienda) && count($tienda) > 0 && $numCatalog > 0 && $numProducts > 0)
        {
            $tienda = $tienda[0];
            $productosAdapter = new Data_Productos();
            $this->view->vistaCatalogo = false;
            $this->view->idCatalogo = false;

            switch($tienda->fuente_productos_iniciales)
            {
                case Data_Catalogos::INICIO_CATALOGO:
                    $idCatalogo = $tienda->valor_productos_iniciales;
                    $this->view->vistaCatalogo = true;
                    $contenido = array('tipo_vista' => 1, 'num_renglones' => '', 'num_columnas' => '', 'num_productos_lista' => '', 'num_productos_slide' => '');
                    $this->view->catalogElement = $this->createTemporalElement($idSitio, ETIPO_CATALOGO, $contenido, $idCatalogo);
                    $this->view->idCatalogo = $idCatalogo;
                    break;
                case Data_Catalogos::INICIO_ETIQUETAS:
                    if(isset($post['edicion']) && $post['edicion'] == 1)
                    {
                        $this->view->edicion = true;
                    }
                    $this->view->vistaCatalogo = true;
                    $contenido = array('tipo_vista' => 1, 'num_renglones' => '', 'num_columnas' => '', 'num_productos_lista' => '', 'num_productos_slide' => '');
                    $this->view->catalogElement = $this->createTemporalElement($idSitio, ETIPO_CATALOGO, $contenido, Data_Catalogos::INICIO_ETIQUETAS);
                    break;
                case Data_Catalogos::INICIO_DESTACADOS:
                    $this->view->vistaCatalogo = true;
                    $contenido = array('tipo_vista' => 1, 'num_renglones' => '', 'num_columnas' => '', 'num_productos_lista' => '', 'num_productos_slide' => '');
                    $this->view->catalogElement = $this->createTemporalElement($idSitio, ETIPO_CATALOGO, $contenido, Data_Catalogos::CATALOGO_DESTACADOS);
                    break;
            }
        }
    }

    public function getInitTagsName($tienda)
    {
        $exceptTagsName = $tienda->valor_productos_iniciales;
        $exceptTagsName = explode(',', $exceptTagsName);
        foreach($exceptTagsName as $k => $tagName)
        {
            if(strlen($tagName) <= 0)
                unset($exceptTagsName[$k]);
            else
                $exceptTagsName[$k] = '"' . $tagName . '"';
        }
        return $exceptTagsName;
    }

    private function cleanGroups($tagsGroups, $tipoCatalogo, $idSitio, $exceptTagsName = '')
    {
        if($tipoCatalogo == Data_Catalogos::INICIO_ETIQUETAS)
        {
            $productosAdapter = new Data_Productos();
            $where = '';
            if(is_array($exceptTagsName) && count($exceptTagsName) > 0)
                $where .= ' t.valor IN ( ' . implode(',', $exceptTagsName) . ' )';
            $productos = $productosAdapter->getProductsByTagsValue($idSitio, $where);
        }
        foreach($tagsGroups as $key => $tagGroup)
        {
            $tagsAdapter = new Data_Tags();
            $idGrupo = $tagGroup['id'];
            switch($tipoCatalogo)
            {
                case Data_Catalogos::INICIO_CATALOGO:
                    $tags = $tagsAdapter->getTagsByGroupIdAndCatalogId($idGrupo, $this->view->idCatalogo, array(), FETCH_ASSOC);
                    break;
                case Data_Catalogos::INICIO_ETIQUETAS:

                    $namesProductos = array();
                    foreach($productos as $i => $producto)
                        $namesProductos[$i] = '"' . $producto->nombre . '"';
                    $tags = $tagsAdapter->getTagsByGroupExceptTagsNames($idGrupo, $exceptTagsName, $namesProductos, FETCH_ASSOC);
                    break;
                case Data_Catalogos::INICIO_DESTACADOS:
                    $tags = $tagsAdapter->getTagsByGroupDestacados($idGrupo, FETCH_ASSOC);
                    break;
            }
            if(is_array($tags) && count($tags) > 0)
            {
                $tagsGroups[$key]['tags'] = $tags;
            }
            else
            {
                unset($tagsGroups[$key]);
            }
        }
        return $tagsGroups;
    }

    private function getTagGroupNames($idSitio)
    {
        $groupsTagsAdapter = new Data_TagsGroups();
        $tagsGroups = $groupsTagsAdapter->getNombreGrupo($idSitio, null, FETCH_ASSOC);
        return $tagsGroups;
    }

    private function getTagGroupsDestacados($idSitio)
    {
        $groupsTagsAdapter = new Data_TagsGroups();
        $tagsGroups = $groupsTagsAdapter->getGroupsDestacados($idSitio, FETCH_ASSOC);
        //$tagsGroups=$tagAdapter->getGroupsSelectedByTags($exceptTagsName,$idSitio);
        return $tagsGroups;
    }

    private function getTagGroupNamesSelected($idSitio, $tagsIni)
    {
        //$exceptTagsName=$tagsExcept;
        $tagAdapter = new Data_Tags();
        $tagsGroups = $tagAdapter->getGroupsSelectedByTags($tagsIni, $idSitio);
        return $tagsGroups;
    }

    public function viewProductAction()
    {
        $this->view->product = false;
        $idProducto = $this->getRequest()->getParam('id');
        if($idProducto)
        {
            $em = $this->getEntityManager('DefaultDb');
            $product = $em->find('DefaultDb_Entities_Product', $idProducto);
            if($product)
            {
                $this->view->product = $product;
                $catalog = $product->getCatalog();
                $this->view->aditional_title = $product->getname() . ' :: ' . $catalog->getTitle();
                $this->view->alternative_description = htmlentities(strip_tags($product->getDescription()));
                $this->view->catalog = $catalog;
                $this->view->user = $catalog->getClient();
                $this->view->category = $this->view->user->getCategory();
            }
        }
    }

    private function cssTemplateAvanzado($toInclude)
    {
        $this->view->getCssManager()->addCss('styleTemplate2' . CSS_POSFIX . '.css');
        $this->view->htmlLayout = $toInclude['html'];
        $this->view->setTemplate('TemplateAvanzado');
    }

    public function getCatalog($idSitio, $index)
    {
        $idCatalog = $this->getParam($index);

        /**
         * Esta validacion es temporal, arreglar el .htaccess para formar correctamente las url's
         * en este momento genera /viewCatalog/nomfix/viewCatalog/idProducto
         */
        if($idCatalog == 'viewCatalog')
            $idCatalog = $this->getParam($index + 1);
        //verificar que existe el catalogo
        $catalog = false;
        if($idCatalog)
        {
            $CatalogAdapter = new Data_Catalogos();
            $catalog = $CatalogAdapter->getCatalogByIdSitioAndCatalog($idSitio, $idCatalog);

            if($catalog)
            {
                $contenido = array('tipo_vista' => 1, 'num_renglones' => '', 'num_columnas' => '', 'num_productos_lista' => '', 'num_productos_slide' => '');
                $this->view->catalogElement = $this->createTemporalElement($idSitio, ETIPO_CATALOGO, $contenido, $idCatalog);
            }
            else
            {
                $this->view->catalogElement = false;
            }
        }
        return $catalog;
    }

    /**
     * Inicializa las variables ocupadas en la vista con valores predeterminados.
     * Posteriormente se dan valores dependiendo de la accion en la que nos encontremos.
     */
    protected function setViewVars()
    {
        $helper = new Helper_DisplayHelper();
        //definimos la accion por la que ingresamos, en produccion no será visible por las reglas del htaccess (index o customDomain)
        $this->view->action = $this->_request->getAction();
        //Indica que no permitiremos la edicion al llamar a los elementos
        $this->view->edition = 0;
        // indica si desplegaremos unicamente un elemento dado
        $this->view->elementMode = $helper->isElementMode($this->getParam(2));
        //Indicará si en la pagina hay elementos galeria
        $this->view->galeria = 0;
        //indica si desplegamos la tienda
        $this->view->tienda = false;
        //Indica si debemos mostrar el carrito de compra
        $this->view->viewCart = false;
        //Indica si desplegaremos los detalles del carrito de compra
        $this->view->reviewCart = false;
        //Indica si estamos en el paso de confirmacion de ordenes
        $this->view->confirmorder = false;
        //Indica si la pagina inicial es de tipo tienda
        $this->view->inicialPageIsStore = false;
        //Si no tiene pagina para los catalogos
        $this->view->nameFixStore = "";
        //define si se muestra o no el menu de cnofiguracion
        $this->view->menuConfig = false;
        //variable para los permisos de edicion
        $this->view->permisos = 0;
        //contendrá los widgets
        $this->view->widgets = array();
        //indica si existe un elemento mapa
        $this->view->addMap = false;
        //ancho default
        $this->view->width1 = '700px';
        //clase default
        $this->view->clase1 = '';
        //indica si estamos en modo preview
        $this->view->preview = false;
        //title complementario para productos o catalogos
        $this->view->aditional_title = false;
        //descripcion alternativa para las vistas de productos
        $this->view->alternative_description = false;
    }

    /**
     * Añade los css necesarios
     */
    protected function setCssFiles()
    {
        $this->view->getCssManager()->addCss('cssgallery/css/galleriffic-2.css');
        $this->view->getCssManager()->addCss('cssgallery/css/galleriffic-5.css');

        $this->view->getJsManager()->addJs('jquery.opacityrollover.js');
        //los siguientes se encontraban en la funcion initialize llamada multiples veces
        $this->view->getCssManager()->addCss('fullcalendar.css');
        $this->view->getCssManager()->addCss('style-1.css');
        $this->view->getCssManager()->addCss('twitter.css');
        $this->view->getCssManager()->addCss('sexylightbox.css');
        $this->view->getCssManager()->addCss('menu-1.css');
        $this->view->getCssManager()->addCss(JQUERY_UI_CSS);
        $this->view->getCssManager()->addCss('widgets-1.css');
        $this->view->getCssManager()->addCss('captify.css');
        $this->view->getCssManager()->addCss('styleSocial-1.css');
        $this->view->getCssManager()->addCss('catalog-1.css');
        $this->view->getCssManager()->addCss('productsviews-1.css');
        $this->view->getCssManager()->addCss('cloudZoom/cloud-zoom.css');
        $this->view->getCssManager()->addCss('agilecarousel/agile_carousel.css');
        $this->view->getCssManager()->addCss('reservations/reservations.css');
    }

    /**
     * Añade los js necesarios. Previamente llamados desde la function initialize
     */
    protected function setJsFiles($element = '')
    {
        switch($element)
        {
            case '':
                $this->view->getJsManager()->addJs(JQUERY_UI_SCRIPT);
                $this->view->getJsManager()->addJs('captify.js');
                $this->view->getJsManager()->addJs('jquery.tools.min.js');
                $this->view->getJsManager()->addJs('jquery.timers-1.1.2.js');
                $this->view->getJsManager()->addJs('templates/display' . JS_POSFIX . '.js');
                $this->view->getJsManager()->addJs('jquery.validate.js');
                $this->view->getJsManager()->addJs('templates/comentarios' . JS_POSFIX . '.js');
                $this->view->getJsManager()->addJs('templates/widgets' . JS_POSFIX . '.js');
                $this->view->getJsManager()->addJs('fullcalendar.js');
                $this->view->getJsManager()->addJs('sexylightbox.v2.3.jquery.js');
                $this->view->getJsManager()->addJs('jquery.tooltip.js');
                $this->view->getJsManager()->addJs('templates/ecommerce' . JS_POSFIX . '.js');
                $this->view->getJsManager()->addJs('cloudZoom/cloud-zoom.1.0.2.js');
                $this->view->getJsManager()->addJs('templates/productsviews' . JS_POSFIX . '.js');
                $this->view->getJsManager()->addJs('jquery.form.js');

                $siteAdapter = new Model_Sitios();
                $site = $siteAdapter->getSiteById($this->_sitio['id']);
                if($site['categoria'] == HOTEL)
                {
                    $this->view->getJsManager()->addJs('Reservaciones/datepicker.js');
                    $this->view->getJsManager()->addJs('Reservaciones/validateDatesReservations.js');
                }
                break;
            case ETIPO_MAP:
            case ETIPO_PRODUCTO:
                $this->view->getJsManager()->addJs('googleMaps.js');
                break;
            case ETIPO_GALLERY:
                $this->view->getJsManager()->addJs('jquery.galleriffic.js');
                break;
            case ElementsController::ETIPO_SLIDE:
                $this->view->getJsManager()->addJs('agilecarousel/agile_carousel.alpha.js');
                break;
        }
    }

    /**
     * En caso de no tener el nombre de la pagina,
     * se infiere que la pagina que se muestra es la de inicio                     
     */
    protected function setInitialPageData()
    {
        $siteAdapter = new Model_Sitios();

        if(strlen($this->_nomFixPage) <= 0)
        {
            $this->view->pagina = $siteAdapter->getPage($this->_sitio['id'], $this->_sitio['id_pagina_inicio'], SELECTOR);
            $this->_nomFixPage = $this->view->pagina['nombre_fix'];
        }
        else
        {
            $this->view->pagina = $siteAdapter->getPageByName($this->_sitio['id'], $this->_nomFixPage);
        }

        if($this->view->pagina && $this->view->pagina['categoria'] == PAGINA_TIENDA)
        {
            $this->view->inicialPageIsStore = true;
        }

        //Obteniendo opciones de la pagina                
        $this->view->currentPag = $this->view->pagina;

        $namePagTienda = $siteAdapter->getPageTiendaByIdSitio($this->_sitio['id'], SELECTOR);

        if($namePagTienda)
            $this->view->nameFixStore = $namePagTienda->nombre_fix;

        $this->view->idSitio = $this->_sitio['id'];
        $this->view->moderacion = $this->_sitio['moderacion'];
        $this->view->template = $this->_sitio['template'];
        $this->view->datos = $this->_sitio;
        $this->view->comentariosDefault = (int) $this->_sitio['comentarios'];
    }

    /**
     * Pone en uno o cero la bandera que define si se carga o no blueprint
     * @param int $tipo_template 
     */
    protected function needBlueprint($tipo_template)
    {
        if($tipo_template != 2)
        {
            //$this->initialize();
            $templateSiteHelper = new Helper_TemplateSites();
            $this->view->loadBlueprint = $tipo_template == 0 ? $templateSiteHelper->blueprint(true) : $templateSiteHelper->blueprint();
        }
    }

    /**
     * Define las clases y variables que dan el ancho de los elementos
     * @param array $sitio 
     */
    protected function setWidthVars($sitio)
    {
        $templateSiteHelper = new Helper_TemplateSites();
        $templateSiteHelper->InitTemplate($sitio);
        $this->view->clase1 = $templateSiteHelper->siteClass();
        $this->view->width1 = $templateSiteHelper->width();
    }

    /**
     * Obtiene las páginas y subpáginas del sitio
     */
    protected function setSitePages()
    {
        $siteAdapter = new Model_Sitios();
        $this->view->paginas = $siteAdapter->getPages($this->_sitio['id'], SELECTOR);

        if($this->view->pagina)
        {
            $this->view->hijos = $siteAdapter->getSons($this->_sitio['id'], $this->view->pagina['id'], SELECTOR);
            $this->view->hermanos = $siteAdapter->getBrothers($this->view->pagina['id_padre'], $this->_sitio['id'], SELECTOR);
            $this->view->idPagina = $this->view->pagina['id'];
        }
    }

    private function setLayoutToTemplate()
    {
        $this->iniTemplate('');
        $paginasAdapter = new Model_VistaPaginas();
        $page = $paginasAdapter->getPageByNameBySitio($this->_nomFixPage, $this->_sitio['id']);
        if(($page && $page->categoria == PAGINA_TIENDA ) || $this->_request->getAction() == 'viewproduct' || $this->_request->getAction() == 'viewCatalog')
        {
            $useOwnBase = false;

            if($this->_sitio['tipo_template'] == 1)
            {
                $t = $this->_sitio['template'];

                if(($t >= 13 && $t <= 22) || $t == 7 || $t == 24 || ($t >= 26 && $t <= 31))
                {
                    $useOwnBase = true;
                }
            }

            if((($this->_sitio['tipo_template'] == 1) && $useOwnBase == false))
                $this->view->setTemplate('TemplateTiendaStores');
            else if((($this->_sitio['tipo_template'] == 0) && ($this->_sitio['template'] == 4)))
                $this->view->setTemplate('TemplateTienda');
        }
    }

    public function checkPassWordPageAjaxAction()
    {
        $this->view->setUseTemplate(false);
        $siteAdapter = new Model_Sitios();
        $correct = false;

        if($this->isPost())
        {
            $post = $this->getPost();
            $typedPassword = $post['typedPass'];
            $pageId = $post['idPage'];
            $siteId = $post['idSite'];
            $this->view->pagina = $siteAdapter->getPage($siteId, $pageId, SELECTOR);

            if($typedPassword != '')
            {
                $password = base64_decode($this->view->pagina['password']);
                if($password == $typedPassword)
                {
                    $correct = true;
                    $this->getElements(); /* Se obtienen los elementos */
                }
            }

            $this->view->correct = $correct;
        }
    }

    public function viewRoomAction()
    {
//        $this->view->elementView = 1;
//        $this->view->useGPlusButton = true;
//        $this->view->useTwitterWidgets = true;
        $sesion = new Model3_Session_Namespace('reservaciones');
        $siteAdapter = new Model_Sitios();
        $fechaInicio = $sesion->fechaInicio;
        $fechaSalida = $sesion->fechaSalida;

        $imagesRoomsAvaliablesAdapter = new Model_Reservaciones_ImagenesHabitaciones();
        $roomsAvaliableAdapter = new Helper_Reservaciones_Reservations();
        $roomsAdapter = new Model_Reservaciones_Habitaciones();

        $imagesRoomsAvaliables = $imagesRoomsAvaliablesAdapter->getImageByIdSitio($this->_sitio['id']);
        $roomsAvailable = $roomsAdapter->getRoomsByIdSitio($this->_sitio['id']);

        //var_dump($imagesRoomsAvaliables);
        $this->view->imagesRoomAvaliable = $imagesRoomsAvaliables;
        $this->view->roomAvaliable = $roomsAvailable;
        $this->setWidthVars($this->_sitio);
        //$this->view->loadBlueprint = true;
        $this->needBlueprint($this->_sitio['tipo_template']);
        $this->view->getJsManager()->addJs('googleMaps.js');

        if($this->isPost())
        {
            $post = $this->getPost();
            $fechaInicio = $post['txtStartDate'];
            $fechaSalida = $post['txtEndDate'];


            $sesion->fechaInicio = $fechaInicio;
            $sesion->fechaSalida = $fechaSalida;
            $this->view->roomReserved = $roomReserved = $roomsAdapter->getRoomsReservedByIdSitio($this->_sitio['id'], $fechaInicio, $fechaSalida);

            //   var_dump($roomReserved);
        }
        else if($fechaInicio != null && $fechaSalida != null)
        {
            $this->view->roomReserved = $roomReserved = $roomsAdapter->getRoomsReservedByIdSitio($this->_sitio['id'], $fechaInicio, $fechaSalida);
        }
        else
        {
            //var_dump('hol');
            $newUrl = 'Display/index/' . $this->view->nomFix . '/' . $this->_nomFixPage;
            $this->redirect($newUrl);
        }

        $this->prepareViewRoom();
    }

    public function prepareViewRoom()
    {
        $this->view->idUsuario = $this->view->showCart = false;
        $siteAdapter = new Model_Sitios();
        $tiendasAdapter = new Model_Reservaciones();
        $helper = new Helper_DisplayHelper();

        $this->view->getCssManager()->addCss('tienda' . CSS_POSFIX . '.css');
        $tienda = $tiendasAdapter->findOne('id_sitio', $this->_sitio['id']);
        $this->view->tienda = $tienda;
        $this->view->idUsuario = $this->_sitio['id_usuario'];
        $this->setLinks($this->_sitio);
        $this->view->showCart = true;
        $this->getSingleProduct($this->_sitio['id'], $this->_baseIndex);
        //obtenemos el contenido de la pag
        $this->getPageContent($this->_nomFixPage, $this->_sitio);
        //Obtenemos las paginas del sitio
        $this->setSitePages();

        $paginasAdapter = new Model_VistaPaginas();
        $idSitio = $this->_sitio['id'];
        $this->view->tipo_template = $this->_sitio['tipo_template'];

        if($this->_sitio['tipo_template'] == 2)
        {
            $templateHelper = new Helper_TemplateAvanzado();
            $layout = '';
            $toInclude = $templateHelper->initAdvancedTemplate($this->_sitio, $layout);
            $this->view->css = $toInclude['css'];
            $this->view->js = $toInclude['js'];
            //Estilos para los sitios de template con plantilla propia,
            //se agregan los estilos de contacto y de comentarios y otros...
            $this->cssTemplateAvanzado($toInclude);
        }
        else
        {
            $this->setLayoutToTemplate();
        }
    }

    public function confirmRoomAction()
    {
        $siteAdapter = new Model_Sitios();
        $this->setWidthVars($this->_sitio);
        //$this->view->loadBlueprint = true;
        if($this->isPost())
        {
            $post = $this->getPost();
            $idRoom = $post['idRoom'];
        }
        else
        {
            $idRoom = $this->getParam(1);
        }
        $roomsAdapter = new Helper_Reservaciones_Reservations();
        $room = $roomsAdapter->getRoomByIdRoom($idRoom);
        $this->view->room = $room;
        $this->view->item = null;
        if($this->getParams() > 1)
        {
            $this->view->item = $this->getParam(2);
        }
        $this->needBlueprint($this->_sitio['tipo_template']);
        $this->view->getJsManager()->addJs('googleMaps.js');
        $this->prepareConfirmRoom();
    }

    public function prepareConfirmRoom()
    {
        $this->view->idUsuario = $this->view->showCart = false;
        $siteAdapter = new Model_Sitios();
        $helper = new Helper_DisplayHelper();

        $this->view->getCssManager()->addCss('tienda' . CSS_POSFIX . '.css');
        $this->view->idUsuario = $this->_sitio['id_usuario'];
        $this->setLinks($this->_sitio);
        $this->view->showCart = true;
        $this->getSingleProduct($this->_sitio['id'], $this->_baseIndex);
        //obtenemos el contenido de la pag
        $this->getPageContent($this->_nomFixPage, $this->_sitio);
        //Obtenemos las paginas del sitio
        $this->setSitePages();

        $paginasAdapter = new Model_VistaPaginas();
        $idSitio = $this->_sitio['id'];
        $this->view->tipo_template = $this->_sitio['tipo_template'];

        if($this->_sitio['tipo_template'] == 2)
        {
            $templateHelper = new Helper_TemplateAvanzado();
            $layout = '';
            $toInclude = $templateHelper->initAdvancedTemplate($this->_sitio, $layout);
            $this->view->css = $toInclude['css'];
            $this->view->js = $toInclude['js'];
            //Estilos para los sitios de template con plantilla propia,
            //se agregan los estilos de contacto y de comentarios y otros...
            $this->cssTemplateAvanzado($toInclude);
        }
        else
        {
            $this->setLayoutToTemplate();
        }
    }

    public function carReservationRoomAction()
    {
        $siteAdapter = new Model_Sitios();
        $this->setWidthVars($this->_sitio);
        //$this->view->loadBlueprint = true;

        $roomsAdapter = new Helper_Reservaciones_Reservations();


        $this->needBlueprint($this->_sitio['tipo_template']);
        $this->view->getJsManager()->addJs('googleMaps.js');
        $this->prepareCarReservationRoom();
    }

    public function prepareCarReservationRoom()
    {
        $this->view->idUsuario = $this->view->showCart = false;
        $siteAdapter = new Model_Sitios();
        $tiendasAdapter = new Data_Tiendas();
        $helper = new Helper_DisplayHelper();

        $this->view->getCssManager()->addCss('tienda' . CSS_POSFIX . '.css');
        $tienda = $tiendasAdapter->findOne('id_sitio', $this->_sitio['id']);
        $this->view->idUsuario = $this->_sitio['id_usuario'];
        $this->setLinks($this->_sitio);
        $this->view->showCart = true;
        $this->getSingleProduct($this->_sitio['id'], $this->_baseIndex);
        //obtenemos el contenido de la pag
        $this->getPageContent($this->_nomFixPage, $this->_sitio);
        //Obtenemos las paginas del sitio
        $this->setSitePages();

        $paginasAdapter = new Model_VistaPaginas();
        $idSitio = $this->_sitio['id'];
        $this->view->tipo_template = $this->_sitio['tipo_template'];

        if($this->_sitio['tipo_template'] == 2)
        {
            $templateHelper = new Helper_TemplateAvanzado();
            $layout = '';
            $toInclude = $templateHelper->initAdvancedTemplate($this->_sitio, $layout);
            $this->view->css = $toInclude['css'];
            $this->view->js = $toInclude['js'];
            //Estilos para los sitios de template con plantilla propia,
            //se agregan los estilos de contacto y de comentarios y otros...
            $this->cssTemplateAvanzado($toInclude);
        }
        else
        {
            $this->setLayoutToTemplate();
        }
    }

    public function categoryAction()
    {
        $categoryId = $this->getRequest()->getParam('id');
        $sessionUserId = Model3_Auth::getCredentials('id');
        $em = $this->getEntityManager('DefaultDb');
        $categoryRepos = $em->getRepository('DefaultDb_Entities_Category');
        $branchesRepos = $em->getRepository('DefaultDb_Entities_BranchesUser');
        $this->view->category = $categoryRepos->find($categoryId);
        $this->view->userId = $sessionUserId;
    }

    public function userAction()
    {
        $companyId = $this->getRequest()->getParam('id');
        $sessionUserId = Model3_Auth::getCredentials('id');

        $em = $this->getEntityManager('DefaultDb');

        $favoriteUserRepos = $em->getRepository('DefaultDb_Entities_FavoriteUsers');
        $favoriteFlagBuyer = false;
        $favoriteFlagSeller = false;

        $client = $em->find('DefaultDb_Entities_User', $sessionUserId);
        $favoriteClient = $em->find('DefaultDb_Entities_User', $companyId);
        $favoriteUsersBuyers = $favoriteUserRepos->findBy(array('client' => $client, 'favoriteClient' => $favoriteClient,'type' => DefaultDb_Entities_FavoriteUsers::FAVORITE_BUYER));
        $favoriteUsersSellers = $favoriteUserRepos->findBy(array('client' => $client, 'favoriteClient' => $favoriteClient, 'type' => DefaultDb_Entities_FavoriteUsers::FAVORITE_SELLER));
        if(is_array($favoriteUsersBuyers) && count($favoriteUsersBuyers))
        {
            $favoriteFlagBuyer = true;
        }
        if(is_array($favoriteUsersSellers) && count($favoriteUsersSellers))
        {
            $favoriteFlagSeller = true;
        }

        $this->view->favoriteFlagBuyer = $favoriteFlagBuyer;
        $this->view->favoriteFlagSeller = $favoriteFlagSeller;
        $this->view->user = $em->find('DefaultDb_Entities_User', $companyId);
        $this->view->category = $this->view->user->getCategory();
        $this->view->getJsManager()->addJs('view/scripts/User/Store/user.js');
        $this->view->getCssManager()->addCss('view/scripts/User/Store/user.css');
        $this->view->getJsManager()->addJsVar('urlAddFavorite', '\'' . $this->view->url(array('module' => '', 'controller' => 'Ajax_Favorite', 'action' => 'addFavorite')) . '\'');
        $this->view->getJsManager()->addJsVar('urlRemoveFavorite', '\'' . $this->view->url(array('module' => '', 'controller' => 'Ajax_Favorite', 'action' => 'removeFavorite')) . '\'');
        $this->view->getJsManager()->addJsVar('commercialName', '\'' . $this->view->user->getCommercialName() . '\'');
    }

}
